package mcom.xmks.bluetooth.mesh.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.graphics.Point;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Display;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.StringRes;

import mcom.xmks.bluetooth.mesh.R;


/**自定义对话框*/
public class UserDialog extends Dialog implements View.OnClickListener {
    private TextView txt_title,txt_msg;
    private EditText editInput;
    private Button buttonCancel,buttonOk;
    private boolean iutput=false;
    private boolean isHideCancel=true;
    private boolean isHideConfirm=true;
    private CharSequence title, message ,iutputData,cancelTitle,confirmTitle;
    //声明两个点击事件，等会一定要为取消和确定这两个按钮也点击事件
    private OnCancelListener cancelListener;
    private OnConfirmListener confirmListener;
    /**
     * 确认按钮侦听
     */
    public interface OnConfirmListener{
        /**
         * 确认按钮按下时触发
         * @param dialog 当前对话框对象
         */
        void onConfirm(DialogInterface dialog);
    }
    public UserDialog(@NonNull Context context) {
        super(context);
        isHideConfirm=false;
        isHideCancel=true;
    }

    public UserDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
        isHideConfirm=false;
        isHideCancel=true;
    }

    protected UserDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        super(context, cancelable,cancelListener);
        this.cancelListener=cancelListener;
        isHideCancel=false;
        isHideConfirm=true;
    }
    /**
     * 构造对话窗实体
     * @param context 上下文
     * @param title 标题
     */
    public UserDialog(@NonNull Context context, @Nullable CharSequence title){
        this(context);
        this.title=title;
        isHideConfirm=false;
        isHideCancel=true;
    }
    /**
     * 构造对话窗实体
     * @param context  上下文
     * @param title 标题
     * @param confirmListener 确认按钮事件侦听
     */
    public UserDialog(@NonNull Context context, @Nullable CharSequence title, @Nullable OnConfirmListener confirmListener){
        this(context,title,confirmListener,  null);
        isHideConfirm=false;
        isHideCancel=true;
    }
    /**
     * 构造对话窗实体
     * @param context  上下文
     * @param title 标题
     * @param confirmListener 确认按钮事件侦听
     * @param cancelListener 取消按钮事件侦听
     */
    public UserDialog(@NonNull Context context, @Nullable CharSequence title, @Nullable OnConfirmListener confirmListener,
                      @Nullable OnCancelListener cancelListener){
        this(context);
        this.title=title;
        this.confirmListener=confirmListener;
        this.cancelListener=cancelListener;
        isHideConfirm=false;
        isHideCancel=false;
    }

    /**
     *  构造对话窗实体
     * @param context 上下文
     * @param title 标题
     * @param message 消息内容
     */
    public UserDialog(@NonNull Context context, @Nullable CharSequence title, @Nullable CharSequence message){
        this(context,title,message,null);
        isHideConfirm=false;
        isHideCancel=true;
    }

    /**
     *  构造对话窗实体
     * @param context  上下文
     * @param title 标题
     * @param message 消息内容
     * @param confirmListener 确认按钮事件侦听
     */
    public UserDialog(@NonNull Context context, @Nullable CharSequence title, @Nullable CharSequence message,
                      @Nullable OnConfirmListener confirmListener){
        this(context,title,message,confirmListener,null);
        isHideConfirm=false;
        isHideCancel=true;
    }

    /**
     *  构造对话窗实体
     * @param context  上下文
     * @param title 标题
     * @param message 消息内容
     * @param confirmListener 确认按钮事件侦听
     * @param cancelListener 取消按钮事件侦听
     */
    public UserDialog(@NonNull Context context, @Nullable CharSequence title, @Nullable CharSequence message,
                      @Nullable OnConfirmListener confirmListener , @Nullable OnCancelListener cancelListener){
        this(context);
        this.title=title;
        this.message=message;
        this.confirmListener=confirmListener;
        this.cancelListener=cancelListener;
        isHideConfirm=false;
        isHideCancel=false;
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_view);
        //设置背景透明，不然会出现白色直角问题
        Window window = getWindow();
        window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        //设置弹窗的宽度
        WindowManager m = getWindow().getWindowManager();
        Display d = m.getDefaultDisplay();
        WindowManager.LayoutParams p =getWindow().getAttributes();
        Point size = new Point();
        d.getSize(size);
        p.width = (int)(size.x * 0.85);//是dialog的宽度为app界面的90%
        getWindow().setAttributes(p);
        initTheme();
        buttonCancel.setOnClickListener(this);
        buttonOk.setOnClickListener(this);
    }

    @Override
    protected void onStart() {
        super.onStart();
        txt_title.setText(this.title);
        if(message!=null && message.length()>0) {
            txt_msg.setVisibility(View.VISIBLE);
            txt_msg.setText(this.message);
        }else{
            txt_msg.setVisibility(View.GONE);
        }
        if( this.isHideCancel){
            buttonCancel.setVisibility(View.GONE);
            this.findViewById(R.id.vertical_line).setVisibility(View.GONE);
        }else{
            if(this.cancelTitle!=null && this.cancelTitle.length()>0)
                buttonCancel.setText(this.cancelTitle);
        }
        if(isHideConfirm){
            buttonOk.setVisibility(View.GONE);
            this.findViewById(R.id.vertical_line).setVisibility(View.GONE);
        }else {
            if (this.confirmTitle != null && this.confirmTitle.length() > 0)
                buttonOk.setText(this.confirmTitle);
        }
        if(iutput){
            editInput.setVisibility(View.VISIBLE);
            editInput.setText(this.iutputData);
            editInput.setFocusable(true);
            if(this.iutputData!=null && this.iutputData.length()>0)
                editInput.setSelection(this.iutputData.length());
        }else{
            editInput.setVisibility(View.GONE);
        }
    }

    /**
     * 设置显示的消息内容
     * @param message
     */
    public void setMessage(@Nullable CharSequence message) {
        this.message=message;
    }
    /**
     * 设置显示的消息内容
     * @param msgId
     */
    public void setMessage(@StringRes int msgId) {
        if(msgId!=0){
            this.message=getContext().getText(msgId);
        }
    }
    @Override
    public void setTitle(@StringRes int titleId) {
        super.setTitle(titleId);
        this.title=getContext().getText(titleId);
    }
    @Override
    public void setTitle(@Nullable CharSequence title) {
        super.setTitle(title);
        this.title=title;
    }
    /**
     * 设置输入数据
     * @param iutputData 输入的数据缓存
     */
    public void setIutputData(@Nullable CharSequence iutputData) {
        this.iutputData=iutputData;
    }
    /**
     * 设置输入数据
     * @param iutputDataId 资源ID
     */
    public void setIutputData(@StringRes int iutputDataId){
        this.iutputData=getContext().getText(iutputDataId);
    }
    /**
     * 获取输入数据
     * @return
     */
    public  @Nullable CharSequence getOutputData(){
        return this.editInput!=null?editInput.getText():iutputData;
    }
    /**
     * 设置取消按钮
     * @param cancel 取消按钮标题
     * @param cancelListener 按钮单击事件侦听
     */
    public void setCancel(@Nullable CharSequence cancel,OnCancelListener cancelListener) {
        this.cancelTitle=cancel;
        this.cancelListener=cancelListener;
        isHideCancel=false;
    }
    /**
     * 设置取消按钮
     * @param textId 字符串资源id
     * @param cancelListener 按钮单击事件侦听
     */
    public void setCancel(@StringRes int textId,OnCancelListener cancelListener) {
        this.cancelTitle=getContext().getText(textId);
        this.cancelListener=cancelListener;
        isHideCancel=false;
    }

    /**
     * 设置确认按钮
     * @param confirm 按钮文本
     * @param confirmListener 确认按钮单击事件
     */
    public void setConfirm(@Nullable CharSequence confirm,OnConfirmListener confirmListener){
        this.confirmTitle=confirm;
        this.confirmListener=confirmListener;
        isHideConfirm=false;
    }

    /**
     * 设置确认按钮
     * @param textId 按钮资源id
     * @param confirmListener 确认按钮单击事件
     */
    public void setConfirm(@StringRes int textId,OnConfirmListener confirmListener){
        this.confirmTitle=getContext().getText(textId);
        this.confirmListener=confirmListener;
        isHideConfirm=false;
    }

    /**
     * 设置在取消对话框时调用的侦听器。
     * @param cancelListener 取消事件侦听器
     */
    public void setOnCancelListener(OnCancelListener cancelListener) {
        this.cancelListener = cancelListener;
        isHideCancel=false;
    }

    /**
     * 设置确认按钮事件侦听
     * @param confirmListener 确认事件侦听器
     */
    public void setOnConfirmListener(OnConfirmListener confirmListener) {
        this.confirmListener = confirmListener;
        isHideConfirm=false;
    }
    /**
     * 设置取消按钮标题
     * @param cancel
     */
    public void setCancelTitle(@Nullable CharSequence cancel){
        this.cancelTitle=cancel;
        isHideCancel=false;
    }
    /**
     * 设置取消按钮标题
     * @param textId
     */
    public void setCancelTitle(@StringRes int textId){
        this.cancelTitle=getContext().getText(textId);
        isHideCancel=false;
    }
    /**
     * 设置确认按钮标题
     * @param confirm
     */
    public void setConfirmTitle(@Nullable CharSequence confirm){
        this.confirmTitle=confirm;
        isHideConfirm=false;
    }
    /**
     * 设置确认按钮标题
     * @param textId
     */
    public void setConfirmTitle(@StringRes int textId){
        this.confirmTitle=getContext().getText(textId);
        isHideConfirm=false;
    }
    /**
     * 显示输入框
     * @param iutput
     */
    public void setIutput(boolean iutput) {
        this.iutput = iutput;
        isHideConfirm=false;
        isHideCancel=false;
    }

    @Override
    public void onClick(View v) {
        if(v.getId()==R.id.buttonCancel) {
            if (cancelListener != null) {
                cancelListener.onCancel(this);
            }
            dismiss();
        }else if(v.getId()==R.id.buttonOk){
            if(confirmListener!=null){
                confirmListener.onConfirm(this);
            }
        }
    }
    /**初始化皮肤*/
    private void initTheme() {
        txt_title=this.findViewById(R.id.textDialogTitle);
        txt_msg=this.findViewById(R.id.textDialogMsg);
        editInput=this.findViewById(R.id.editInput);
        buttonCancel=this.findViewById(R.id.buttonCancel);
        buttonOk=this.findViewById(R.id.buttonOk);
    }
}
